package xyz.csmcool.imserver.config;

import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 什锦
 * @Package xyz.csmcool.imchat.config
 * @since 2022/8/14 14:08
 */

public class WsSessionManager {
    /**
     * 保存连接 session 的地方
     */
    private static ConcurrentHashMap<String, WebSocketSession> SESSION_POOL = new ConcurrentHashMap<>();

    private static ConcurrentHashMap<Integer, Map<Integer,WebSocketSession>> ROOM_SESSION_POOL = new ConcurrentHashMap<>();

    /**
     * 添加 session
     * @param key
     */
    public static void add(String key, WebSocketSession session) {
        // 添加 session
        SESSION_POOL.put(key, session);
    }

    public static void add(Integer uid,Integer roomId,WebSocketSession session) {
        Map<Integer, WebSocketSession> roomUsers = ROOM_SESSION_POOL.get(roomId);
        if (roomUsers == null) {
            synchronized (WsSessionManager.class) {
                if(roomUsers == null) {
                    HashMap<Integer,WebSocketSession> users = new HashMap<>();
                    ROOM_SESSION_POOL.put(roomId,users);
                }
            }
            Map<Integer, WebSocketSession> users = ROOM_SESSION_POOL.get(roomId);
            users.put(uid,session);
            ROOM_SESSION_POOL.put(roomId,users);
        } else {
            roomUsers.put(uid,session);
            ROOM_SESSION_POOL.put(roomId,roomUsers);
        }
    }

    /**
     * 删除 session,会返回删除的 session
     * @param key
     * @return
     */
    public static WebSocketSession remove(String key) {
        // 删除 session
        return SESSION_POOL.remove(key);
    }

    public static WebSocketSession remove(Integer uid,Integer roomId) {
        // 删除 session
        return ROOM_SESSION_POOL.get(roomId).remove(uid);
    }

    /**
     * 删除并同步关闭连接
     * @param key
     */
    public static void removeAndClose(String key) {
        WebSocketSession session = remove(key);
        if (session != null) {
            try {
                // 关闭连接
                session.close();
            } catch (IOException e) {
                // todo: 关闭出现异常处理
                e.printStackTrace();
            }
        }
    }


    public static void removeAndClose(Integer uid,Integer roomId) {
        WebSocketSession session = remove(uid, roomId);
        if (session != null) {
            try {
                // 关闭连接
                session.close();
            } catch (IOException e) {
                // todo: 关闭出现异常处理
                e.printStackTrace();
            }
        }
    }

    /**
     * 获得 session
     * @param key
     * @return
     */
    public static WebSocketSession get(String key) {
        // 获得 session
        return SESSION_POOL.get(key);
    }

    public static WebSocketSession get(Integer uid,Integer roomId) {
        // 获得 session
        return ROOM_SESSION_POOL.get(roomId).get(uid);
    }

    public static Map<Integer, WebSocketSession> get(Integer roomId) {
        // 获得 session
        return ROOM_SESSION_POOL.get(roomId);
    }
}
